home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14315 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  56 lines

  1. Path: pangea.Stanford.EDU!karish
  2. From: karish@pangea.Stanford.EDU (Chuck Karish)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Looking for date & distance routines
  5. Date: 13 Apr 1996 09:29:07 GMT
  6. Organization: Mindcraft, Inc.
  7. Message-ID: <4kns53$ge2@nntp.Stanford.EDU>
  8. References: <4kn6e7$ba9@ux.accesscom.net>
  9. NNTP-Posting-Host: pangea.stanford.edu
  10.  
  11. In article <4kn6e7$ba9@ux.accesscom.net>, Wisdom <wisdom@wisdom.com> wrote:
  12. >
  13. >I'm in search of two sets of routines:
  14. >
  15. >  1.  Code to calculate the amount of days between two time_t-type
  16. >date variables, and a routine to return a date x days from a specified
  17. >time_t structure.
  18.  
  19. On what type of system?  If your C library has mktime(), it will
  20. convert data in a struct tm into a time_t, at which point you can
  21. subtract and divide by 84600.
  22.  
  23. >  2.  A routine that will calculate the distance in miles, between two
  24. >sets of lattitude/longitude coordinates.
  25.  
  26. This function gives the result in kilometers:
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <math.h>
  31.  
  32.  
  33. #define DEGTORAD        57.29578
  34. #define EARTHRAD        6371.
  35.  
  36. double
  37. distance(lat1, long1, lat2, long2)
  38. double lat1, long1, lat2, long2;
  39. {
  40.     double colat1, colat2, ang1, ang2, alpha;
  41.  
  42.     /* algorithm from John Osler */
  43.     alpha=fabs((long2-long1)/DEGTORAD) ;
  44.     colat1=(90.-lat2)/DEGTORAD;
  45.     colat2=(90.-lat1)/DEGTORAD;
  46.  
  47.     ang1=cos(colat2)*cos(colat1);
  48.     ang2=sin(colat2)*sin(colat1)*cos(alpha);
  49.     return(acos(ang1+ang2)*EARTHRAD);
  50.  
  51. }
  52. --
  53.  
  54.     Chuck Karish          karish@mindcraft.com
  55.     (415) 323-9000 x117   karish@pangea.stanford.edu
  56.